import type { AppRouteRecordRaw, AppRouteModule } from '/@/router/types';
import { LAYOUT } from '/@/router/constant';
import { WELCOME_ROUTE, PAGE_NOT_FOUND_ROUTE, REDIRECT_ROUTE, ACCOUNT_PAGE } from '/@/router/routes/basic';
import { mainOutRoutes } from '/@/router/routes/mainOut';

import { PageEnum } from '/@/enums/pageEnum';
import { t } from '/@/hooks/web/useI18n';

const modules = import.meta.glob('./modules/**/*.ts', { eager: true });

const routeModuleList: AppRouteModule[] = [];

// 加入到路由集合中
Object.keys(modules).forEach((key) => {
  const mod = (modules as Recordable)[key].default || {};
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  routeModuleList.push(...modList);
});

export const asyncRoutes = [PAGE_NOT_FOUND_ROUTE, WELCOME_ROUTE, ...routeModuleList];

export const RootRoute: AppRouteRecordRaw = {
  id: 'Root',
  path: '/',
  name: 'Root',
  redirect: PageEnum.BASE_HOME,
  meta: {
    title: 'Root',
  },
};

export const LoginRoute: AppRouteRecordRaw = {
  id: 'Login',
  path: '/login',
  name: 'Login',
  component: () => import('/@/views/sys/login/Login.vue'),
  meta: {
    title: t('routes.basic.login'),
  },
};

// 第三方登录页面路由
export const ThirdLoginRoute: AppRouteRecordRaw = {
  id: 'third-auth-login',
  path: '/thirdAuth/login',
  name: 'third-auth-login',
  component: () => import('/@/views/sys/login/ThirdLoginPage.vue'),
  meta: {
    title: t('routes.oauth2.login'),
  },
};


/**
 * 【通过token直接静默登录】流程办理登录页面 中转跳转
 */
export const TokenLoginRoute: AppRouteRecordRaw = {
  id: 'TokenLoginRoute',
  path: '/tokenLogin',
  name: 'TokenLoginRoute',
  component: () => import('/@/views/sys/login/TokenLoginPage.vue'),
  meta: {
    title: '带token登录页面',
    ignoreAuth: true,
  },
};



export const OnlinePreviewRoute: AppRouteModule = {
  id: 'inOnlinePreview',
  path: '/onlinePreview',
  name: 'inOnlinePreview',
  component: LAYOUT,
  redirect: '/onlinePreview/index',
  meta: {
    icon: 'simple-icons:about-dot-me',
    title: t('routes.dashboard.about'),
    orderNo: 100000,
  },
  children: [
    {
      id: 'onlinePreviewPage',
      path: 'index',
      name: 'onlinePreviewPage',
      component: () => import('/@/views/onlinePreview/index.vue'),
      meta: {
        title: t('routes.dashboard.about'),
        icon: 'simple-icons:about-dot-me',
      }
    },
  ],
};

// Basic routing without permission
export const basicRoutes = [
  LoginRoute, RootRoute, REDIRECT_ROUTE, PAGE_NOT_FOUND_ROUTE, TokenLoginRoute, ThirdLoginRoute,
  OnlinePreviewRoute, ACCOUNT_PAGE,
  ...mainOutRoutes
];
